//
//  VCOfSpecialistDisabuse.m
//  LCOnePsychology
//
//  Created by li cheng on 16/9/5.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "VCOfSpecialistDisabuse.h"
#import "BaseTableView.h"
#import "MJMacro.h"
#import "TVCellOfSpecialistDisabuse.h"
#import "ZWYNetTool.h"
#import "DataOfSpecialistDisabuse.h"
#import "OffOfDataSpecialistDisabuse.h"
#import "OnOfDataSpecialistDisabuse.h"
#import "RootClassOfSpecialistDisabuse.h"
#import "TagsOfOffDataSpecialistDisabuse.h"
#import "TagsOfOnDataSpecialistDisabuse.h"
#import "TeacherOfOffDataSpecialistDisabuse.h"
#import "TeacherOfOnDataSpecialistDisabuse.h"
#import "UIImageView+WebCache.h"
#import "BaseView.h"
@interface VCOfSpecialistDisabuse ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) RootClassOfSpecialistDisabuse *rootClassOfSpecialistDisabuse;
@property (nonatomic, strong) BaseTableView *tableViewOfSpecialistDisabuse;
@end

@implementation VCOfSpecialistDisabuse

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"专家解惑";
    // 数据处理
    [self handleData];
    //创建 tableView
    [self createTableView];
}
#pragma mark - 数据处理
- (void)handleData{
    [ZWYNetTool GET:@"http://yiapi.xinli001.com/v2/yi/get-jiehuo-list.json?offset=0&limit=10&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&sign=e50dcdf83dbf8a2ed23988021d362615&_version=4.0&token=0d4eeb8732f9ae2c73401002570ceb93" andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _rootClassOfSpecialistDisabuse = [RootClassOfSpecialistDisabuse modelDic:result];
        [self.tableViewOfSpecialistDisabuse reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
}
#pragma mark - 创建 tableView
- (void)createTableView{
    self.tableViewOfSpecialistDisabuse = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 64 - 49) style:UITableViewStylePlain];
    self.tableViewOfSpecialistDisabuse.backgroundColor =[UIColor colorWithWhite:0.937 alpha:1.000];
    [self.view addSubview:_tableViewOfSpecialistDisabuse];
    self.tableViewOfSpecialistDisabuse.delegate = self;
    self.tableViewOfSpecialistDisabuse.dataSource = self;
    [self.tableViewOfSpecialistDisabuse registerNib:[UINib nibWithNibName:@"TVCellOfSpecialistDisabuse" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"TVCellOfSpecialistDisabuse"];
}
#pragma mark - tableView section 数量的确定
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
#pragma mark - tableView 的代理方法 row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return _rootClassOfSpecialistDisabuse.data.on.count;
    } else {
        return _rootClassOfSpecialistDisabuse.data.off.count;
    }
}
#pragma mark - tableView row 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return SCREEN_HEIGHT * 0.50;
}
#pragma mark - tableView 的代理方法 cell 的赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TVCellOfSpecialistDisabuse *cell = [tableView dequeueReusableCellWithIdentifier:@"TVCellOfSpecialistDisabuse" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.avatarImageVIew.layer.cornerRadius = 10;
    cell.clipsToBounds = YES;
    if (indexPath.section == 0) {
        
        [cell.coverImagView sd_setImageWithURL:[NSURL URLWithString:_rootClassOfSpecialistDisabuse.data.on[indexPath.row].cover] placeholderImage:[UIImage imageNamed:@"JustAMoment"]];
        cell.titleLabel.text = _rootClassOfSpecialistDisabuse.data.on[indexPath.row].title;
        cell.nicknameLabel.text = _rootClassOfSpecialistDisabuse.data.on[indexPath.row].teacher.nickname;
        [cell.avatarImageVIew sd_setImageWithURL:[NSURL URLWithString:_rootClassOfSpecialistDisabuse.data.on[indexPath.row].teacher.avatar] placeholderImage:[UIImage imageNamed:@"JustAMoment"]];
        cell.briefLabel.text = _rootClassOfSpecialistDisabuse.data.on[indexPath.row].teacher.brief;
        cell.contentLabel.text = _rootClassOfSpecialistDisabuse.data.on[indexPath.row].content;
        NSMutableString *singleStringName = [NSMutableString string];
        NSMutableString *finalStringName = [NSMutableString string];
        for (TagsOfOnDataSpecialistDisabuse *tagsOfOnDataSpecialistDisabuse in _rootClassOfSpecialistDisabuse.data.on[indexPath.row].tags ) {
            singleStringName = [NSMutableString stringWithFormat:@"%@", tagsOfOnDataSpecialistDisabuse.name];
            finalStringName = [NSMutableString stringWithFormat:@"%@ %@", finalStringName, singleStringName];
        }
        cell.tagsLabel.text = finalStringName;
        cell.questionnumLabel.text = [NSString stringWithFormat:@"%@提问", _rootClassOfSpecialistDisabuse.data.on[indexPath.row].questionnum];
    } else {
        [cell.coverImagView sd_setImageWithURL:[NSURL URLWithString:_rootClassOfSpecialistDisabuse.data.off[indexPath.row].cover] placeholderImage:[UIImage imageNamed:@"JustAMoment"]];
        cell.titleLabel.text = _rootClassOfSpecialistDisabuse.data.off[indexPath.row].title;
        cell.nicknameLabel.text = _rootClassOfSpecialistDisabuse.data.off[indexPath.row].teacher.nickname;
        [cell.avatarImageVIew sd_setImageWithURL:[NSURL URLWithString:_rootClassOfSpecialistDisabuse.data.off[indexPath.row].teacher.avatar] placeholderImage:[UIImage imageNamed:@"JustAMoment"]];
        cell.briefLabel.text = _rootClassOfSpecialistDisabuse.data.off[indexPath.row].teacher.brief;
        cell.contentLabel.text = _rootClassOfSpecialistDisabuse.data.off[indexPath.row].content;
        NSMutableString *singleStringName = [NSMutableString string];
        NSMutableString *finalStringName = [NSMutableString string];
        for (TagsOfOnDataSpecialistDisabuse *tagsOfOnDataSpecialistDisabuse in _rootClassOfSpecialistDisabuse.data.off[indexPath.row].tags ) {
            singleStringName = [NSMutableString stringWithFormat:@"%@", tagsOfOnDataSpecialistDisabuse.name];
            finalStringName = [NSMutableString stringWithFormat:@"%@ %@", finalStringName, singleStringName];
        }
        cell.tagsLabel.text = finalStringName;
        cell.questionnumLabel.text = [NSString stringWithFormat:@"%@回答", _rootClassOfSpecialistDisabuse.data.off[indexPath.row].answernum];
    }
    
    return cell;
}
#pragma mark - 分区头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}
#pragma mark - 自定义分区头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    BaseView *sectionHeaderView = [[BaseView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    sectionHeaderView.backgroundColor = [UIColor colorWithWhite:0.937 alpha:1.000];
    UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 3, 20)];
    blueView.backgroundColor = [UIColor blueColor];
    [sectionHeaderView addSubview:blueView];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH / 2, 44)];
    NSString *stringTitle = [NSString string];
    switch (section) {
        case 0:
        {
            stringTitle = @"正在进行";
        }
            break;
        default:{
            stringTitle = @"精彩回顾";
        }
            break;
    }
    titleLabel.text = stringTitle;
    [sectionHeaderView addSubview:titleLabel];
    return sectionHeaderView;
}
#pragma mark - 其他, 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
